package com.news.web.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.Admin;
import com.news.service.AdminService;
import com.news.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminAdminUpdateServlet
 */
@WebServlet("/AdminAdminUpdateServlet")
public class AdminAdminUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String adminName = request.getParameter("adminName");
		String adminPass = request.getParameter("adminPass");
		String adminIdStr = request.getParameter("adminId");
		// 检查 所有值 是否为空
		if (adminName == null || adminPass == null) {
			// 处理 值为空的情况
			System.out.println("Str is null");
			request.setAttribute("adminUpadteBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		System.out.println(adminIdStr);
		int adminId = 0;
		try {
			adminId = Integer.parseInt(adminIdStr);
		} catch (NumberFormatException e) {
			// 可以处理参数不合法的情况，例如跳转到错误页面或者返回错误信息
			// 处理 adminIdStr 无法解析为整数的情况
			e.printStackTrace(); // 记录日志或者向用户显示错误信息
			request.setAttribute("adminUpadteBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}

		// 实例化 admin 对象
		Admin admin = new Admin();
		admin.setAdminName(adminName);
		admin.setAdminPass(adminPass);
		admin.setAdminId(adminId);

		AdminService adminService = new AdminServiceImpl();

		// 调用新增方法
		Boolean adminUpadteBoolean = adminService.updateAdmin(admin);
		// 执行查询方法
		List<Admin> adminQueryLikeList = adminService.likeSelectListAdmin(admin);

		// 将数据存入 request 属性中
		request.setAttribute("adminQueryLikeList", adminQueryLikeList);
		request.setAttribute("adminUpadteBoolean", adminUpadteBoolean);
		request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
